# -*- coding: utf-8 -*-
# @Time : 2021/9/13 11:11
# @Author : 阿怪
# @Email : 673691277@qq.com
# @File : business_page.py
# @Project : crm自动化测试项目实战
'''商机页面类'''

from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep
import time

today = time.strftime('%Y-%m-%d')  # 获取当前日期
from utils.Echo_defs.test_random import get_data
from utils.util import HOST2

class BusinessPage(BasePage):
    '''商机页面类'''

    #定义定位器
    url = '{}/index.php?m=business'.format(HOST2)
    add_opportunity_locator = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')  # 添加商机定位器
    client_locator = (By.CSS_SELECTOR,'#form1 > table > tbody > tr:nth-child(2) > td:nth-child(4)')  # 点击‘客户’输入框
    newest_client_locator = (By.CSS_SELECTOR,'#datas > tr:nth-child(1) > td:nth-child(1) > input[type=radio]:nth-child(1)')  # 选中首条客户
    confirm_locator = (By.XPATH,'/html/body/div[10]/div[3]/div/button[1]/span')   #添加客户界面，【确认】定位器
    busname_locator = (By.XPATH, '//*[@id="name"]')  #商机名定位器
    submit_locator = (By.NAME, 'submit')   #编辑商机界面，【保存】定位器
    estimate_price_locator = (By.ID, 'estimate_price') #预计成交价定位器
    check_all_locator = (By.ID,'check_all')   #全选按钮
    delete_locator = (By.XPATH,'//*[@id="delete"]')   #删除按钮



    #操作元素方法
    def add_business(self):
        '''点击添加商机'''
        element = self.driver.find_element(*self.add_opportunity_locator)
        element.click()

    def click_clientinput(self):
        '''点击【客户输入框】'''
        element = self.driver.find_element(*self.client_locator)
        element.click()

    def click_client(self, client=newest_client_locator):
        '''默认点击首条客户'''
        element1 = self.driver.find_element(*client)  # 默认选择首条客户
        element1.click()
        sleep(1)

    def click_confirm(self):
        '''添加客户界面，点击【确认】'''
        element2 = self.driver.find_element(*self.confirm_locator)
        element2.click()
        sleep(1)


    def input_businessname(self, businessname):
        '''输入商机名'''
        element3 = self.driver.find_element(*self.busname_locator)
        element3.send_keys(businessname + str(get_data()))  # 输入商机名，每次需要更换
        sleep(1)
    def input_price(self, estimate_price):
        '''输入商机金额'''
        self.driver.find_element(*self.estimate_price_locator).send_keys(estimate_price)


    def click_submit(self,):
        '''点击【保存】'''
        self.driver.find_element(*self.submit_locator).click()
        sleep(1)


    def edit_business(self,businessname,estimate_price):
        '''编辑商机页面'''
        self.click_clientinput()
        self.click_client()
        self.click_confirm()
        self.input_businessname(businessname)
        self.input_price(estimate_price)
        self.click_submit()


    def click_checkall(self):
        '''点击全选 '''
        self.driver.find_element(*self.check_all_locator).click()

    def click_delete(self):
        '''点击删除'''
        self.driver.find_element(*self.delete_locator).click()

    def clear_business(self):
        '''删除商机信息'''
        self.click_checkall()
        self.click_delete()
        self.alert()




